﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PolePosition.v3.Desktop.Editors.Controls
{
    public partial class Meter : DialogEditBase
    {
        public Meter()
        {
            InitializeComponent();
            BindCombos();
        }

        private ProjectCommon2.Generics.EntityCollection<EntityLibrary.VendorDistributionNotesXMeterEntity> m_MeterNotes;
        public Common.Enumerations.DataTypes DataType { get; private set; }

        private void BindCombos()
        {
            if (Program.LookupTableData != null)
            {

                listSelectorNotes.DisplayMember = EntityLibrary.DistributionMeterNoteEnumerations.Columns.Name.ToString();
                listSelectorNotes.ValueMember = EntityLibrary.DistributionMeterNoteEnumerations.Columns.MeterNoteId.ToString();
                if (EditAllowed)
                    listSelectorNotes.DataSource = Program.LookupTableData.DistributionMeterNotes;
                else
                    listSelectorNotes.Clear();
            }
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public object Data
        {
            get
            {
                return bindingSourceMeter.DataSource;
            }
            set
            {
                bindingSourceMeter.SuspendBinding();
                SetupDatabindings(value);
                bindingSourceMeter.DataSource = value;
                bindingSourceMeter.ResumeBinding();
            }
        }

        [DefaultValue(false)]
        public bool EditAllowed { get; set; }

        private void SetupDatabindings(object data)
        {
            if (data.GetType() == typeof(PolePosition.v3.EntityLibrary.VendorDistributionMeterEntity))
            {
                DataType = Common.Enumerations.DataTypes.ProjectsDistributionInspection;

                this.bindingSourceMeter.DataSource = typeof(PolePosition.v3.EntityLibrary.VendorDistributionMeterEntity);

                this.textBoxMeterNumber.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSourceMeter, "Number", true, DataSourceUpdateMode.OnPropertyChanged));
                this.textBoxComments.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSourceMeter, "Comments", true, DataSourceUpdateMode.OnPropertyChanged));

                EntityLibrary.VendorDistributionMeterEntity _Meter;
                _Meter = data as EntityLibrary.VendorDistributionMeterEntity;

                LoadNotesAsynch(_Meter.Vendor_Distribution_Meters_MeterId.Value);
            }
        }

        private void LoadNotesAsynch(int resultId)
        {
            listSelectorNotes.LoadingVisible = true;
            backgroundWorkerNotes.RunWorkerAsync(resultId);
        }


        private void backgroundWorkerNotes_DoWork(object sender, DoWorkEventArgs e)
        {
            //System.Threading.Thread.Sleep(1000);

            if (DataType == Common.Enumerations.DataTypes.ProjectsDistributionInspection)
                m_MeterNotes = ServiceProxy.UtilityService.GetDistributionInspectionProjectNotesForMeter((int)e.Argument);
           
            if (backgroundWorkerNotes.CancellationPending)
                e.Cancel = true;
        }

        private void backgroundWorkerNotes_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            listSelectorNotes.LoadingVisible = false;

            if (e.Cancelled)
                return;

            if (e.Error != null)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("A Data Retrieval Error has occurred attempting to load the Notes. Please close editor and try again.", "Data Retrieval Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                listSelectorNotes.Enabled = false;
                return;
            }

            if (DataType == Common.Enumerations.DataTypes.ProjectsDistributionInspection)
            {
                if (m_MeterNotes != null)
                {
                    if (EditAllowed)
                    {
                        var _NoteValues = from notes in m_MeterNotes
                                          select notes.Vendor_Distribution_NoteFk.Value;
                        listSelectorNotes.SetSelectedValues<int>(_NoteValues.ToList<int>());
                    }
                    else
                    {
                        if (m_MeterNotes.Count() > 0)
                        {
                            //this will only show selected items
                            var _NoteValues = from notes in m_MeterNotes
                                              join lookupdata in Program.LookupTableData.DistributionMeterNotes
                                              on notes.Vendor_Distribution_NoteFk equals lookupdata.MeterNoteId
                                              select lookupdata;

                            listSelectorNotes.ReadOnlyNoneSelectedVisible = false;
                            listSelectorNotes.DataSource = _NoteValues.ToList();
                        }
                        else
                        {
                            listSelectorNotes.ReadOnlyNoneSelectedVisible = true;
                        }
                    }


                }
            }
           


            listSelectorNotes.Enabled = true;
        }


    }
}
